{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5410e5b1-269a-4ad8-b4e6-0ece4ab92971",
   "metadata": {},
   "source": [
    "查找匹配的图案"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0694ac31-7153-4bf4-90da-a6d1e9fd4aae",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c9e911b6-d836-4620-8de3-e0b2f7564f5f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取扑克图案 查找方板图案\n",
    "image = cv2.imread('./img/poker.jpg')\n",
    "gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2942d041-5c22-4610-be6f-4e867c814db9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将要查找的图案写入template中\n",
    "template = gray[75:105,235:265]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2376cc79-c265-4309-bf4b-53fed3f87026",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "match = cv2.matchTemplate(gray,template,cv2.TM_CCOEFF_NORMED)\n",
    "# 找出匹配系数大于0.9的匹配点\n",
    "locations = np.where(match>=0.9)\n",
    "w, h = template.shape[0:2]\n",
    "for p in zip(*locations[::-1]):\n",
    "    x1, y1 = p[0], p[1]\n",
    "    x2, y2 = x1 + w, y1 + h\n",
    "    cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)\n",
    "\n",
    "cv2.imshow(\"image\", image)\n",
    "cv2.waitKey()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
